#! /usr/bin/env python
# coding:utf-8

'''
本模块用于统计及格率
'''
import function
def sql_cat(sql):
    function.cursor.execute(sql)
    return function.cursor.fetchall()
def count_stu():
    GUI ='''-------------------------------------
    |语文   |数学   |英语
-------------------------------------'''
    print '''
         各科平均分与及格率
    ===========================
 '''
    sql = 'SELECT chinese,math,english FROM studata'
    function.cursor.execute(sql)
    stu_grade = sql_cat(sql)                   # 取出各科成绩
    cn_grade = 0                                # 语文成绩
    math_grade = 0                              # 数学成绩
    en_grade = 0                                # 英语成绩
    num = len(stu_grade)*1.0                    # 总人数
    sql_cn = 'SELECT chinese FROM studata WHERE chinese > 59'
    sql_math = 'SELECT math FROM studata WHERE math > 59'
    sql_en = 'SELECT english FROM studata WHERE english > 59'
    len_cn = len(sql_cat(sql_cn))
    len_math = len(sql_cat(sql_math))
    len_en = len(sql_cat(sql_en))
    for i in stu_grade:                        # 计算总分
        (chinese, math, english) = i
        cn_grade += chinese
        math_grade += math
        en_grade += english
    print '各科平均分为：'
    print GUI
    print'    |%-6.4s |%-6.4s |%.4s' % (cn_grade/num, math_grade/num, en_grade/num)
    print '====================================='
    print '各科及格率为：'
    print GUI
    print'    |%-6.4s |%-6.4s |%.4s' % (len_cn/num, len_math/num, len_en/num)



